package model;

import protocol.JT808_2011.*;
import utils.ExceptionUtils;
import utils.HexUtils;
import utils.StringUtils;
import java.util.List;

/**
 * JT/T 905.2 - 2014
 */
public class Protocol808_2011Model extends IModel {

    private final static int Protocol_Analysis = 0;//数据内容解析
    private final static int Protocol_Check = 1;//数据校验

    @Override
    protected void initTypes(List<String> list) {
        list.add(Protocol_Analysis,"数据内容解析");
        list.add(Protocol_Check,"数据校验");
    }

    @Override
    public String change(String input) {
        try{
            input = StringUtils.formatProtocol(input);

            byte[] list = HexUtils.toHexBytes(input);
            JT808_2011Cmd cmd = new JT808_2011Cmd(list);

            if (getType() == Protocol_Check){
                return checkProtocolStr(cmd,list);
            }

            byte order = cmd.order();

            switch (order) {
                case 0x00:
                    return new Handle808_2011JT00().handle(cmd);
                case 0x01:
                    return new Handle808_2011JT01().handle(cmd);
                case 0x02:
                    return new Handle808_2011JT02().handle(cmd);
                case 0x03:
                    return new Handle808_2011JT03().handle(cmd);
                case 0x05:
                    return new Handle808_2011JT05().handle(cmd);
                case 0x07:
                    return new Handle808_2011JT07().handle(cmd);
                case 0x08:
                    return new Handle808_2011JT08().handle(cmd);
                case 0x09:
                    return new Handle808_2011JT09().handle(cmd);
                case 0x0A:
                    return new Handle808_2011JT0A().handle(cmd);
                case (byte) 0x80:
                    return new Handle808_2011JT80().handle(cmd);
                case (byte) 0x81:
                    return new Handle808_2011JT81().handle(cmd);
                case (byte) 0x82:
                    return new Handle808_2011JT82().handle(cmd);
                case (byte) 0x83:
                    return new Handle808_2011JT83().handle(cmd);
                case (byte) 0x84:
                    return new Handle808_2011JT84().handle(cmd);
                case (byte) 0x85:
                    return new Handle808_2011JT85().handle(cmd);
                case (byte) 0x86:
                    return new Handle808_2011JT86().handle(cmd);
                case (byte) 0x87:
                    return new Handle808_2011JT87().handle(cmd);
                case (byte) 0x88:
                    return new Handle808_2011JT88().handle(cmd);
                case (byte) 0x89:
                    return new Handle808_2011JT89().handle(cmd);
                case (byte) 0x8A:
                    return new Handle808_2011JT8A().handle(cmd);
                default:
                    return "未匹配的命令字" + (cmd.readOrder() );
            }

        }catch (Exception e){
            return "数据校验失败\n"+ ExceptionUtils.getExceptionMsg(e);
        }
    }

    private String checkProtocolStr(JT808_2011Cmd cmd, byte[] data) {
        StringBuilder builder = new StringBuilder();

        if (data[0] != 0x7e){
            builder.append("数据包头错误(正常包头：7e)："+HexUtils.toHexStr(new byte[]{data[0],data[1]}));
            builder.append("\r\n");
            return builder.toString();
        }

        if (data[data.length - 1] != 0x7e){
            builder.append("数据包尾错误(正常包尾：7e)："+HexUtils.toHex(data[data.length - 1],1));
            builder.append("\r\n");
            return builder.toString();
        }

        builder.append("命令字："+cmd.readOrder());
        builder.append("\r\n");

        builder.append("数据区："+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

}
